package com.wbdgj.ui.booking;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.AllOrderAdapter;
import com.wbdgj.adapter.OrderAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.base.BaseFragment;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultBalanceModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class Order04Fragment extends BaseFragment {
    private Context context;

    @BindView(R.id.demo_listview)
    PullListView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;
    private int page = 1;
    private int totalPage;
    private ArrayList<LinkedTreeMap> linkedTreeMapList;
    private AllOrderAdapter orderAdapter;
    private DecimalFormat decimalFormat;
    private String ID_CARD;

    @Override
    protected int getLayout() {
        return R.layout.ui_order_01;
    }

    @Override
    protected void onInit() {
        context = getActivity();

        ID_CARD = getArguments().getString("ID_CARD");
        decimalFormat = new DecimalFormat("###################.###########");
        listAll();
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        listAll();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if ((page*10) > totalPage){
                    ToastUtils.toastShort("已无更多");
                }else{
                    page = page + 1;
                    listAll();
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });

        myPullListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                if(!orderAdapter.hasNoData){
//                    if ((orderAdapter.getItem(i).get("status")+"").equals("已确认")){
//                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定取消订单吗？", 1);
//                        confirmDialog.show();
//                        confirmDialog.setCanceledOnTouchOutside(false);
//                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
//                            @Override
//                            public void doConfirm() {
//                                confirmDialog.dismiss();
//                                cancel_room_order(orderAdapter.getItem(i).get("book_number")+"");
//                            }
//
//                            @Override
//                            public void doCancel() {
//                                confirmDialog.dismiss();
//                            }
//                        });
//                    }
                }
            }
        });

        listAll();
    }

    @Override
    public void fetchData() {

    }

    private void listAll() {
        Call<ResultBalanceModel> call = HttpAdapter.getSerives().my_room_order(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD, page+"", "10", "3");
        call.enqueue(new OnResponseListener<ResultBalanceModel>(context) {
            @Override
            protected void onSuccess(ResultBalanceModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap pageLinkedTreeMap = (com.google.gson.internal.LinkedTreeMap) userModelBaseResultModel.getPager();
                    totalPage = Integer.parseInt(decimalFormat.format(pageLinkedTreeMap.get("total")));

                    linkedTreeMapList = new ArrayList<>();
                    linkedTreeMapList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    if (page == 1){
                        orderAdapter = new AllOrderAdapter(context, linkedTreeMapList);
                        myPullListView.setAdapter(orderAdapter);
                    }else{
                        orderAdapter.loadMoreListView(linkedTreeMapList);
                        orderAdapter.notifyDataSetChanged();
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }


}
